//
//  HNSH_NewFriendCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/27.
//

#import "HNSH_NewFriendCell.h"

@interface HNSH_NewFriendCell ()
@property (nonatomic, strong)UIImageView * imgView;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * contentLabel;
@property (nonatomic, strong)UIButton * btn;
@property (nonatomic, strong)UIButton * ignoreBtn;
@end

@implementation HNSH_NewFriendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor appWhiteLabelColor];
        self.backgroundColor = [UIColor appWhiteLabelColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.contentView addSubview:self.imgView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.btn];
    [self.contentView addSubview:self.ignoreBtn];
}

- (void)addAllLayout{
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).offset(12);
        make.right.mas_equalTo(-80);
        make.height.mas_equalTo(22);
        make.top.mas_equalTo(7);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(-164);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(2);
        make.height.mas_equalTo(20);
    }];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(32);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.ignoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-88);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(32);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.contentView addGestureRecognizer:longPress];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)press {
    if (press.state == UIGestureRecognizerStateBegan) {
        if (self.longPressBlock) {
            self.longPressBlock();
        }
    }
}

- (void)btnAction{
    if (self.acceptBlock) {
        self.acceptBlock(self);
    }
}

- (void)ignoreBtnAction {
    if (self.ignoreBlock) {
        self.ignoreBlock(self);
    }
}

//MARK: Setter Methods
- (void)setModel:(HNSH_ChatFriendModel *)model {
    _model = model;
    
    HNSH_ChatFriendUserModel * user = model.user;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:user.portrait] placeholderImage:[UIImage imageNamed:@"base_user_header_icon"]];
    self.nameLabel.text = user.display_name.length ? user.display_name : user.name;
    self.contentLabel.text = model.message;
    if (model.status == 11) {
        self.btn.userInteractionEnabled = YES;
        [self.btn setTitle:@"同意" forState:UIControlStateNormal];
        [self.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor appGreenColor];
        self.ignoreBtn.hidden = NO;
    } else {
//        10 : 申请中
//        11 : 待验证
//        12 : 已过期
//        20 : 已同意
//        21 : 已拒绝
        [self.btn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        if (model.status == 12) {
            [self.btn setTitleColor:[UIColor colorWithHexString:@"#F73525"] forState:UIControlStateNormal];
            [self.btn setTitle:@"已失效" forState:UIControlStateNormal];
        } else if (model.status == 20) {
            [self.btn setTitle:@"已添加" forState:UIControlStateNormal];
        } else if (model.status == 21) {
            [self.btn setTitle:@"已拒绝" forState:UIControlStateNormal];
        }
        self.btn.userInteractionEnabled = NO;
        self.btn.backgroundColor = [UIColor clearColor];
        self.ignoreBtn.hidden = YES;
    }
}

//MARK: Getter Methods
- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 8;
        _imgView.layer.masksToBounds = YES;
        _imgView.image = [UIImage imageNamed:@"icon_placeholder"];
    }
    return _imgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _nameLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:13];
        _contentLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _contentLabel;
}

- (UIButton *)btn{
    if (!_btn) {
        _btn = [UIButton new];
        [_btn setTitle:@"接受" forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn.titleLabel.font = [UIFont systemFontOfSize:14];
        _btn.layer.cornerRadius = 6;
        _btn.layer.masksToBounds = YES;
        _btn.backgroundColor = [UIColor appGreenColor];
        [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn;
}

- (UIButton *)ignoreBtn{
    if (!_ignoreBtn) {
        _ignoreBtn = [UIButton new];
        [_ignoreBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        [_ignoreBtn setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _ignoreBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _ignoreBtn.layer.cornerRadius = 6;
        _ignoreBtn.layer.masksToBounds = YES;
        _ignoreBtn.backgroundColor = [UIColor colorWithHexString:@"#FEEAE8"];
        [_ignoreBtn addTarget:self action:@selector(ignoreBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _ignoreBtn.hidden = YES;
    }
    return _ignoreBtn;
}

@end
